import Print from "./print";

// 数字字符串校验正则
export const numRegex = /^[-+]?\d+(\.\d+)?$/;
// 科学计数法数字字符串校验
export const scienceNumRegex = /^[-+]?\d+(\.\d+)?e[-+](\d+)$/;

/**
 * @func 补零
 * @param {Number} len 补零长度
 */
export const createZeroStr = (len: number) => {
  return new Array(len).fill(0).reduce((a, b) => `${a}${b}`, "");
};

/**
 * @func 去除数组尾部多于0
 * @param {Array} arr 待去尾部零的数组
 */
export const deleteZeroArr = (arr: string[] | number[]): string[] => {
  const str = arr.reduce((a, b) => `${a}${b}`, "") as string;
  if (str === "0") {
    return ["0"];
  }
  const result = str.replace(/(0+)$/, "");
  if (!result) {
    return ["0"];
  }
  return result.split("");
};

/**
 * 获取小数位数
 * @param {String} floatStr
 */
export const getDecimalLength = (floatStr: string) => {
  return floatStr.replace(/.*\.(.*)$/, "$1").length;
};

/**
 * @return  :{decimalPlaces:Number,value:String}
 * @func 获取数组中最长小数位数的项，并返回当前项以及该项的小数位数
 * @param {Array} args Array类型，待对比的字符串数字
 */
export const getMaxDecimal = (a: string[]) => {
  const returnValue = {
    decimalPlaces: 0,
    value: "",
  };

  if (!a || !a.length) {
    Print.error("请输入参数");
    return { ...returnValue };
  }

  let max = returnValue.decimalPlaces;
  let maxValue = returnValue.value;
  for (let i = 0; i < a.length; i++) {
    const item = String(a[i]);
    if (item.indexOf(".") > -1) {
      const curentLength = item.replace(/.*\.(.*)/, "$1").length;
      if (curentLength > max) {
        max = curentLength;
        maxValue = item;
      }
    }
  }
  returnValue.decimalPlaces = max;
  returnValue.value = maxValue;
  return { ...returnValue };
};

/**
 * @func 比较两数大小
 * @param {*} param1
 * @param {*} param2
 */
export const compare = (param1: any[], param2: number[] | string[]) => {
  const a = param1?.length ? param1 : [];
  const b = param2?.length ? param2 : [];
  if (a.length > b.length) {
    return 1;
  }
  if (a.length < b.length) {
    return -1;
  }
  if (a.length === b.length) {
    for (let index = a.length - 1; index >= 0; index--) {
      if (a[index] > b[index]) {
        return 1;
      } else if (a[index] < b[index]) {
        return -1;
      }
    }
  }
  return 0;
};

/**
 *
 * @param {Array} arr 待转换成字符串数组
 * @param {Number} decimalLength
 * @param {Number} prefix
 */
export const getResult = (
  arr: string[],
  decimalLength: number,
  prefix: number
) => {
  if (!arr) {
    return "0";
  }
  let resultArr = [...arr];
  if (decimalLength) {
    if (resultArr.length > decimalLength) {
      resultArr.splice(decimalLength, 0, ".");
    } else {
      // 小数点位数过长需要向后补零
      resultArr = resultArr.concat(
        createZeroStr(decimalLength - resultArr.length + 1).split("")
      );
      resultArr.splice(decimalLength, 0, ".");
    }
  }
  resultArr.reverse();
  let resultStr = resultArr.reduce((a, b) => `${a}${b}`, "");
  // 去掉末尾0
  resultStr = resultStr.replace(/(\.0+)$/, "");
  // 将前置多余 0000.xxx替换成0.xxx
  resultStr = resultStr.replace(/^(0+\.)/, "0.");
  resultStr = resultStr.replace(/^(0+([1-9]))/, "$2");
  if (prefix && prefix === -1) {
    resultStr = `-${resultStr}`;
  }
  if (resultStr.indexOf(".") > -1) {
    resultStr = resultStr.replace(/(0+)$/, "");
  }
  return resultStr;
};

/**
 * @func 将科学计数法数字字符串，转换成普通数字字符串
 * @param {Number|String} scienceNumber
 */
export const exchangeScienceString = (scienceNumber: string) => {
  if (scienceNumber && scienceNumRegex.test(String(scienceNumber))) {
    // 判断正负
    const [, prefix = "", scienceStr] =
      String(scienceNumber).match(/^([-+])?(.*)/) || [];
    // 拆分 整数、小数、指数
    const [, , integerStr, floatStr = "", powSymbol, powStr] =
      scienceStr.match(/((\d+)(\.\d+)?)e([-+])(\d+)/) || [];
    // 与指数对比的字符串
    const compareStr = powSymbol === "-" ? integerStr : floatStr.substr(1);
    const compareArr = compareStr.split("");
    // 小数位数或者整数位数大于指数，无需补0
    if (compareStr.length > Number(powStr)) {
      if (powSymbol === "-") {
        compareArr.splice(-Number(powStr), 0, ".");
      } else {
        compareArr.splice(Number(powStr), 0, ".");
      }
      let positionStr = compareArr.reduce((a, b) => `${a}${b}`, "");
      if (compareStr.length === Number(powStr)) {
      }
      return powSymbol === "-"
        ? `${prefix}${positionStr}${floatStr.substr(1)}`
        : `${prefix}${integerStr}${positionStr}`;
    } else {
      //补零操作
      const supZeroArr = new Array(Number(powStr) - compareStr.length).fill(0);
      const supZeroStr = supZeroArr.reduce((a, b) => `${a}${b}`, "");
      if (powSymbol === "-") {
        return `${prefix}0.${supZeroStr}${integerStr}${floatStr.substr(1)}`;
      }
      return `${prefix}${integerStr}${floatStr.substr(1)}${supZeroStr}`;
    }
  } else {
    Print.error(`非科学计数法数字字符串：${scienceNumber}`);
    return scienceNumber;
  }
};
